# -*- coding: utf-8 -*-
import os
import sys
# add up one level dir into sys path
sys.path.append(os.path.abspath(os.path.dirname(
    os.path.dirname(os.path.dirname(__file__)))))
os.environ['DJANGO_SETTINGS_MODULE'] = 'webservices.settings'
from datetime import datetime
from common.es_core.es_interface import es_search_interface
from common.mg_core.mg import MONGODB

check_start_time = datetime(2019, 7, 25)

qk_user = MONGODB['qk']['market_user_info'].find({"registered_at": {"$gte": check_start_time}})
total = MONGODB['qk']['market_user_info'].count({"registered_at": {"$gte": check_start_time}})

for idx, user in enumerate(qk_user):
    query_dsl = {
        "query": {
            "bool": {
                "filter": [
                    {"term": {"_event_id": "recharge"}},
                    {"term": {"_user_id": user['user_id']}}
                ]
            }
        },
        "size": 0,
        "aggs": {
            "recharge": {
                "sum": {
                    "field": "recharge_price"
                }
            }
        }
    }
    res = es_search_interface('dark3', query_dsl)
    if res['aggregations']['recharge']['value'] - (user['total_recharge'] + user['recharge']) > 0.01:
        print(user['user_id'], res['aggregations']['recharge']['value'], user['total_recharge'] + user['recharge'])
    print('{} pass {}/{}'.format(user['user_id'], idx, total))
